<html>
<meta chars="utf-8">
<meta content="width=device-width,initial-scale=1">
<title>贪吃蛇</title>
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="css/icon/iconfont.css">
<script src="scripts/jquery-1.11.0.min.js"></script>
<script src="scripts/snake.js"></script>
<script src="scripts/main.js"></script>
<script src="css/icon/iconfont.js"></script>

<body>
    <div class="main-container">
        <div id="score-tab" class="score-tab">
            <p class="score-show-tab">总分数：<span id="score-show" class="score-show">0</span></p>
        </div>
        <div id="game-container" class="game-container">
             
        <div class="button-zone">
            <input type="button" value="开 始" class="btn-begin" data-start="0" id="btn_begin"/>            
        </div>
        </div>
    </div>
</body>
<script>
    var gameStart=0;
    var snakeInterval=null;//移动的循环器
    var snakeMoveSpeed=300;//移动速度,毫秒
    $(function(){       
        //初始化游戏
        gameInit();
        //按钮开始
        $("#btn_begin").on('click',function(){
            var isStart=$("#btn_begin").attr('data-start')||0;
            if(snakeInterval!=null)clearInterval(snakeInterval);
            console.log('按钮')
            if(isStart==0){
                gameStart=1;
                $("#btn_begin").val('暂 停');
                $("#btn_begin").attr('data-start','1')
                snakeInterval=setInterval(function(){snakeMove();},snakeMoveSpeed); 
            }else{
                gameStart=0;
                $("#btn_begin").attr('data-start','0')
                $("#btn_begin").val('开 始');
            }
        })
    });
    //键盘操作
    document.onkeydown=function(eve){
        //
        if(e&&(e.keyCode==37||e.keyCode==38||e.keyCode==39||e.keyCode==40)){
            e.preventDefault();
        }
        var e=eve||window.event||arguments.callee.caller.arguments[0];
        if(e&&e.keyCode==37){//Left    
            snakeFutureDirection=4;
            snakeLocationArray[0].newMoveDir=4;     
        }else if(e&&e.keyCode==38){//Up
            snakeFutureDirection=1;
            snakeLocationArray[0].newMoveDir=1;   
        }else if(e&&e.keyCode==39){//Right
            snakeFutureDirection=2;
            snakeLocationArray[0].newMoveDir=2;   
        }else if(e&&e.keyCode==40){//Down
            snakeFutureDirection=3;
            snakeLocationArray[0].newMoveDir=3;   
        }else if(e&&e.keyCode==32){//Spece
        console.log('点击_'+gameStart);
            //暂停/开始
            if(gameStart==1){
                gameStart=0;
                $("#btn_begin").val('开 始');
                $("#btn_begin").attr('data-start','0')
                if(snakeInterval!=null)clearInterval(snakeInterval);
            }else if(gameStart==0){
                gameStart=1;
                $("#btn_begin").val('暂 停');
                $("#btn_begin").attr('data-start','1')
                if(snakeInterval!=null)clearInterval(snakeInterval);
                snakeInterval=setInterval(function(){snakeMove();},snakeMoveSpeed);
            }
        }
    }
</script>
</html>